package com.maiyuren.mdict_flutter_plugin;

import androidx.annotation.NonNull;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;
import java.util.Vector;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/** MdictFlutterPlugin */
public class MdictFlutterPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "mdict_flutter_plugin");
    channel.setMethodCallHandler(this);
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    int i = 3;
    try {
      if (call.method.equals("getPlatformVersion")) {
        result.success("Android " + android.os.Build.VERSION.RELEASE);
      }
      else if (call.method.equals("lookforMdict")) {
        JSONObject args = new JSONObject((String) call.arguments);
        String path = (String) args.get("path");
        String word = (String) args.get("word");
        String r = MdictLib.getInstance().lookup(path, word);
        result.success(r);
      }
      else if (call.method.equals("releaseMdict")) {
        JSONObject args = new JSONObject((String) call.arguments);
        String path = (String) args.get("path");
        MdictLib.getInstance().release(path);
        result.success(true);
      }
      else if (call.method.equals("getNameMdict")) {
        JSONObject args = new JSONObject((String) call.arguments);
        String path = (String) args.get("path");
        String name = MdictLib.getInstance().getName(path);
        result.success(name);
      }
      else if (call.method.equals("suggestMdict")) {
        JSONObject args = new JSONObject((String) call.arguments);
        String path = (String) args.get("path");
        String word = (String) args.get("word");
        int maxCount = (int) args.get("maxCount");
        List<String> r = new Vector<>();
        MdictLib.getInstance().suggest(path, word, maxCount, r);
        result.success(r);
      }
      else if (call.method.equals("getSizeMdict")) {
        JSONObject args = new JSONObject((String) call.arguments);
        String path = (String) args.get("path");
        result.success(MdictLib.getInstance().size(path));
      } else if (call.method.equals("getResourceMdict")) {
        JSONObject args = new JSONObject((String) call.arguments);
        String path = (String) args.get("path");
        String url = (String) args.get("url");
        byte[] data = MdictLib.getInstance().getResource(path, url);
        result.success(data);
      }
      else {
        result.notImplemented();
      }
    } catch (JSONException ex) {
      result.success(null);
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
